# -*- coding: utf-8 -*-
# @Time    : 2023/9/19 15:10
# @Author  : floraachy
# @File    : create_project_page.py
# @Software: PyCharm
# @Desc:

# 标准库导入
# 第三方库导入
import allure
# 本地应用/模块导入
from utils.base_utils.base_page import BasePage


class CreateProjectPage(BasePage):
    """创建项目页"""
    # 项目名称
    locator_project_name_input = "xpath=//input[@id='NewWorkForm_name']"
    # 项目标识
    locator_project_identifier_input = "xpath=//input[@id='NewWorkForm_repository_name']"
    # 项目简介
    locator_project_desc_input = "xpath=//textarea[@id='NewWorkForm_description']"
    # gitignore
    locator_project_gitignore_checkbox = "xpath=//input[@id='NewWorkForm_ignoreFlag']"
    locator_project_gitignore_input = "xpath=//div[@id='NewWorkForm_ignore']"
    locator_project_gitignore_li_value = "//li[text()='{}']"
    # 开源许可证
    locator_project_license_checkbox = "xpath=//input[@id='NewWorkForm_licenseFlag']"
    locator_project_license_input = "xpath=//div[contains(text(), '请选择开源许可证')]"
    locator_project_license_li_value = "//li[text()='{}']"
    # 项目类别
    locator_project_category_checkbox = "xpath=//input[@id='NewWorkForm_categoreFlag']"
    locator_project_category_input = "xpath=//div[text()='请选择项目类别']"
    locator_project_category_li_value = "//li[text()='{}']"
    # 项目语言
    locator_project_language_checkbox = "xpath=//input[@id='NewWorkForm_languageFlag']"
    locator_project_language_input = "xpath=//div[text()='请选择项目语言']"
    locator_project_language_li_value = "//li[text()='{}']"
    # 项目私有复选框
    locator_private_checkbox = "#NewWorkForm_private"
    # 创建项目按钮
    locator_create_project_btn = "xpath=//span[text()='创建项目']/parent::button"

    @allure.step("访问新建项目页面：/projects/deposit/new")
    def navigate(self):
        """
        访问新建项目页面
        """
        self.visit("/projects/deposit/new")

    @allure.step("输入项目名称: {name}")
    def input_project_name(self, name):
        """
        输入项目名称
        """
        # 输入项目名称
        self.input(locator=self.locator_project_name_input, text=name)

    @allure.step("输入项目标识: {identifier}")
    def input_project_identifier(self, identifier):
        """
        输入项目标识
        """

        # 输入项目标识
        self.input(locator=self.locator_project_identifier_input, text=identifier)

    @allure.step("输入项目简介: {desc}")
    def input_project_desc(self, desc):
        """
        输入项目简介
        """

        # 输入项目简介
        self.input(locator=self.locator_project_desc_input, text=desc)

    @allure.step("选择.gitignore: {gitignore}")
    def choose_project_gitignore(self, gitignore):
        """
        选择.gitignore
        """
        # 选择gitignore
        self.click(locator=self.locator_project_gitignore_checkbox)
        self.click(locator=self.locator_project_gitignore_input)
        # 根据需要勾选的gitignore的值，构建完整的 XPath 定位
        locator_gitignore_value = self.locator_project_gitignore_li_value.format(gitignore)
        self.click(locator=locator_gitignore_value)

    @allure.step("选择开源许可证: {certificate}")
    def choose_project_licence(self, certificate):
        """
        选择开源许可证
        """

        # 选择开源许可证
        self.click(locator=self.locator_project_license_checkbox)
        self.click(locator=self.locator_project_license_input)
        # 根据需要勾选的license的值，构建完整的 XPath 定位
        locator_license_value = self.locator_project_license_li_value.format(certificate)
        self.click(locator=locator_license_value)

    @allure.step("选择项目类别: {category}")
    def choose_project_category(self, category):
        """
        选择项目类别
        """
        self.click(locator=self.locator_project_category_checkbox)
        self.click(locator=self.locator_project_category_input)
        # 根据需要勾选的category的值，构建完整的 XPath 定位
        locator_category_value = self.locator_project_category_li_value.format(category)
        self.click(locator=locator_category_value)

    @allure.step("选择项目语言: {language}")
    def choose_project_language(self, language):
        """
        选择项目语言
        """
        self.click(locator=self.locator_project_language_checkbox)
        self.click(locator=self.locator_project_language_input)
        # 根据需要勾选的category的值，构建完整的 XPath 定位
        locator_language_value = self.locator_project_language_li_value.format(language)
        self.click(locator=locator_language_value)

    @allure.step("勾选复选框，将项目设为私有")
    def check_private_checkbox(self):
        """
        勾选复选框，将项目设为私有
        """
        self.check(locator=self.locator_private_checkbox)

    @allure.step("点击【创建项目】按钮，提交新建项目表单")
    def click_create_project_btn(self):
        """
        点击：创建项目 按钮，提交新建项目表单
        """
        self.click(locator=self.locator_create_project_btn)
        self.page.wait_for_timeout(3000)  # 超时时间是3s

    # --------------------- 流程 -------------------------------------
    @allure.step(
        "新建私有项目：输入项目名称：{name}，项目标识：{identifier}，项目简介：{desc}，选择.gitignore：{gitignore}， 开源许可证：{certificate}，勾选私有项目，项目语言：{language}，项目类别：{category}，点击'创建项目'按钮")
    def create_private_project_flow(self, name, identifier, desc=None, gitignore=None, certificate=None, category=None,
                                    language=None):
        """
        新建私有项目：
        输入项目名称，项目标识，项目简介，选择.gitignore， 开源许可证，勾选私有项目，项目语言，项目类别，点击"创建项目"按钮
        """
        self.input_project_name(name)
        self.input_project_identifier(identifier)
        if desc:
            self.input_project_desc(desc)
        if gitignore:
            self.choose_project_gitignore(gitignore)
        if certificate:
            self.choose_project_licence(certificate)
        self.check_private_checkbox()
        if category:
            self.choose_project_category(category)
        if language:
            self.choose_project_language(language)

        self.click_create_project_btn()

    @allure.step(
        "新建公开项目：输入项目名称：{name}，项目标识：{identifier}，项目简介：{desc}，选择.gitignore：{gitignore}， 开源许可证：{certificate}，项目语言：{language}，项目类别：{category}，点击'创建项目'按钮")
    def create_public_project_flow(self, name, identifier, desc=None, gitignore=None, certificate=None, category=None,
                                   language=None):
        """
        新建公开项目：
        输入项目名称，项目标识，项目简介，选择.gitignore， 开源许可证，项目语言，项目类别，点击"创建项目"按钮
        """
        self.input_project_name(name)
        self.input_project_identifier(identifier)
        if desc:
            self.input_project_desc(desc)
        if gitignore:
            self.choose_project_gitignore(gitignore)
        if certificate:
            self.choose_project_licence(certificate)
        if category:
            self.choose_project_category(category)
        if language:
            self.choose_project_language(language)
        self.click_create_project_btn()
